package com.tmsb.bigscreen.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;


/**
 * 单例模式，只会加载一次配置文件
 * 懒汉式,采用双锁机制，安全且在多线程情况下能保持高性能
 * 支持传入多个hosts创建多个redisCluster
 *
 * @author shihb
 * @version 2017-11-2
 */
public class RedisCluster {

	private static List<JedisCluster> redisClusterList;
	private static List<String> redisHostList;
	private volatile  static RedisCluster instance =null;
	private RedisCluster() {
		this.initRedis();
	}

	/**
	 * 获取JedisCluster list 与传入redisHostList一一对应（传入null返回null,传入空返回空）
	 *
	 * @return
	 */
	public List<JedisCluster> getRedisClusterList() {
		return redisClusterList;
	}


	/**
	 * 此方法只会加载一次，请确认redisHost是否全部传入
	 *
	 * @param redisHostList
	 *            redis的集群信息 list
	 * @return 单一RedisClusterNew实例
	 */
	public  static RedisCluster getInstance(List<String> redisHostList) {
		if (instance==null){
			synchronized(RedisCluster.class){
				if(instance==null){
					RedisCluster.redisHostList = redisHostList;
					instance=new RedisCluster();
				}
			}
		}
		return instance;

	}

	/**
	 * 重载方法传入可传入多个redis集群信息
	 * @param redisHosts 传入多个redis集群信息
	 * @return
	 */
	public  static RedisCluster getInstance(String...redisHosts) {
		if (instance==null){
			synchronized(RedisCluster.class){
				if(instance==null){
					List<String> list=new ArrayList<String>();
					for(String s:redisHosts){
						list.add(s);
					}
					RedisCluster.redisHostList=list;
					instance=new RedisCluster();
				}
			}
		}
		return instance;
	}

	public static void closeAllCluster() {
		if (redisClusterList == null) {
			return;
		}
		for (JedisCluster j : redisClusterList) {
			if (j != null) {
				try {
					j.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}



	private void initRedis() {
		final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
		// 设置最大连接数
		poolConfig.setMaxTotal(5);
		// 设置最大空闲连接数
		poolConfig.setMaxIdle(3);
		// 设置最小空闲连接数
		poolConfig.setMinIdle(1);
		// 设置获取连接的最大等待时间
		poolConfig.setMaxWaitMillis(15000);
		// 在检测空闲对象线程检测到对象不需要移除时，是否检测对象的有效性
		poolConfig.setTestWhileIdle(true);
		// 设置连接被回收前的最大空闲时间
		// poolConfig.setMinEvictableIdleTimeMillis(5 * 60000);
		// 设置检测线程的运行时间间隔
		poolConfig.setTimeBetweenEvictionRunsMillis(60000);
		// 设置检测线程每次检测的对象数
		poolConfig.setNumTestsPerEvictionRun(1);
		if (redisHostList == null) {
			return;
		}
		RedisCluster.redisClusterList = new ArrayList<JedisCluster>();
		for (String clusterHosts : redisHostList) {
			if (clusterHosts == null) {
				RedisCluster.redisClusterList.add(null);
			} else {
				String[] hosts = clusterHosts.split(",");
				Set<HostAndPort> jedisNodes = new HashSet<HostAndPort>();
				for (String host : hosts) {
					if (!(null == host && "".equals(host))) {
						String[] hp = host.split(":");
						jedisNodes.add(new HostAndPort(hp[0], Integer.valueOf(hp[1])));
					}
				}
				RedisCluster.redisClusterList
						.add(new JedisCluster(jedisNodes, 30000, 300000, 6, "tmsb@bigdata2020", poolConfig));
			}

		}
	}

}
